package zho.remotecontrol.text;

import android.app.ProgressDialog;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.CardView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;

import org.json.JSONObject;

import java.net.SocketTimeoutException;

import butterknife.Bind;
import zho.remotecontrol.R;
import zho.remotecontrol.socket.SocketClient;

import static zho.remotecontrol.Util.Utility.randomID;

public class RegisterActivity extends AppCompatActivity {

    private EditText etUserName;
    private EditText etPassword;
    private Button btnSignUp;
    private FloatingActionButton fab;
    private SocketClient client;

    ProgressDialog pd;
//    Handler connecthandler = new Handler(){
//        @Override
//        public void handleMessage(Message msg) {
//            Log.d("handleMessage: ","接收成功");
//            //根据获取的状态来判断当前插座的亮灭状态
//            switch (msg.what){
//                //是否用户连接成功
//                case SocketClient.SOCKET_CONNTENTC:
//                    socket_mode = SocketClient.SOCKET_CONNTENTC;
//                    pd.dismiss();
//                    //清除文本
//                    userNameEdit.setText("");
//                    userPasswordEdit.setText("");
//                    device_Ip_Port.setText("当前连接地址: "+IP+":"+PORT);
//                    connect_console.startAnimation(mHiddenAction);
//                    connect_console.setVisibility(View.GONE);
//                    connected_console.startAnimation(mShowAction);
//                    connected_console.setVisibility(View.VISIBLE);
//                    break;
//                case SocketClient.SOCKET_BUTTON_STATUS:
//                    socket_mode = SocketClient.SOCKET_BUTTON_STATUS;
//                    String contentData = (String)msg.obj;
//                    try {
//                        JSONObject packetObject = new JSONObject(contentData);
//                        JSONObject contentObject = packetObject.getJSONObject("result");
//                        String status = contentObject.getString("status");
//                        String id = contentObject.getString("id");
////                        if(id.equals(device_ID)){}
//                        if(status.equals("1")){
//                            button_mode = BUTTON_ON;
//                            powerButton.setBackgroundResource(R.drawable.power_on);
//                        }else if(status.equals("0")){
//                            button_mode = BUTTON_OFF;
//                            powerButton.setBackgroundResource(R.drawable.power_off);
//                        }
//                    }catch (Exception e){
//                        e.printStackTrace();
//                    }
//                    break;
//                case SocketClient.SOCKET_NOTCONNECT:
//                    Toast.makeText(Main.this,"用户登陆失败",Toast.LENGTH_SHORT).show();
//                    pd.dismiss();
//                    break;
//                default:
//                    break;
//            }
//        }
//    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
        register();
    }

    private void initView(){
        etUserName = (EditText)findViewById(R.id.username);
        etPassword = (EditText)findViewById(R.id.password);
        btnSignUp = (Button)findViewById(R.id.sign_up_button);
        fab = (FloatingActionButton)findViewById(R.id.fab);
    }

    private void register(){
        btnSignUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String userName = etUserName.getText().toString();
                String userPassword = etPassword.getText().toString();
                User user = new User(userName,userPassword);
                String contentUser = new Gson().toJson(user);

                pd = ProgressDialog.show(RegisterActivity.this,"注册","请等待..");
                //pd.setContentView(R.layout.dialog_logging_in);
                pd.show();
                try{
                    client = new SocketClient(Main.IP,Main.PORT);
                    if(client.isConnected()){
                        Log.d("sendMessage: ","连接数据成功");
                        client.sendMessage(String.valueOf(randomID()));
                        client.sendMessage(contentUser);
//                    client.setHandler(connecthandler);
//                    new Thread(client).start();

                    }
                }catch (SocketTimeoutException e){
                    e.printStackTrace();
                    Log.i("xfh", "getIp...SocketTimeoutException");
                    Toast.makeText(RegisterActivity.this,"注册失败，服务器连接超时",Toast.LENGTH_SHORT).show();
                    pd.dismiss();
                }catch(Exception e){
                    e.printStackTrace();
                }


            }
        });
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
